package myicesscheduler.web;

import static org.junit.Assert.*;

import java.io.File;

import myicesscheduler.web.MyFileUtil.StructuredPathname;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( loader = MockServletContextWebContextLoader.class, locations = { "/WebContent/WEB-INF/myicesscheduler-servlet.xml" } )
public class MyFileUtilTest extends AbstractJUnit4SpringContextTests
{
   private static final String delim = String.valueOf( File.separatorChar );
   private String absoluteFolderName;
   private String absolutePath;

   @BeforeClass
   static public void beforeClass()
   {
      System.setProperty( MyFolderUtil.uploadFolder_folderPropertyName, "." + delim + "WebContent"
            + delim + "WEB-INF" + delim + "upload" );

      System.setProperty( MyFolderUtil.backupFolder_folderPropertyName, "." + delim + "WebContent"
            + delim + "WEB-INF" + delim + "backup" );
   }

   @Before
   public void before()
   {
      final String absolutePathOf_Upload = MyFolderUtil.uploadFolder.getAbsolutePath();
      assertNotNull( absolutePathOf_Upload );
      assertTrue( isThereFolder( absolutePathOf_Upload ) );

      absoluteFolderName = absolutePathOf_Upload + delim + "someFolder";
      absolutePath = absoluteFolderName + delim + "someFile";

      MyFileUtil.removeFile( absolutePath );
      MyFileUtil.removeFolderIfEmpty( absoluteFolderName );
   }

   @Test
   public void fixtureTest()
   {}

   @Test
   public void getAbsolutePathOfWebInf()
   {
      final File upload = new File( MyFolderUtil.uploadFolder.getAbsolutePath() );
      assertTrue( upload.exists() );
      assertTrue( upload.canWrite() );
   }

   @Test
   public void OverflowOfInt_shouldNotThrowException()
   {
      try
      {
         int i = Integer.MAX_VALUE;
         i += 100;
      } catch( Exception e )
      {
         fail();
      }
   }

   @Test
   public void getDigestedValue()
   {
      final String digestedValue1 = MyFileUtil.getDigestedValue( "content1".getBytes() );
      final String digestedValue2 = MyFileUtil.getDigestedValue( "content2".getBytes() );
      assertFalse( digestedValue1.equalsIgnoreCase( digestedValue2 ) );
   }

   @Test
   public void createAndRemoveFolder()
   {
      assertFalse( isThereFolder( absoluteFolderName ) );
      MyFileUtil.createFolder( absoluteFolderName );
      assertTrue( isThereFolder( absoluteFolderName ) );
      MyFileUtil.removeFolderIfEmpty( absoluteFolderName );
      assertFalse( isThereFolder( absoluteFolderName ) );
   }

   @Test
   public void RemoveFolderOnlyWhenEmpty()
   {
      final String subFolder = absoluteFolderName + delim + "subFolder";
      MyFileUtil.createFolder( absoluteFolderName );
      MyFileUtil.createFolder( subFolder );

      MyFileUtil.removeFolderIfEmpty( absoluteFolderName );
      assertTrue( isThereFolder( absoluteFolderName ) ); // Here.

      MyFileUtil.removeFolderIfEmpty( subFolder );
      assertFalse( isThereFolder( subFolder ) );
      MyFileUtil.removeFolderIfEmpty( absoluteFolderName );
      assertFalse( isThereFolder( absoluteFolderName ) );
   }

   @Test
   public void storeFile()
   {
      byte[] data = new byte[] { 1, 2, 3, 4 };
      assertFalse( isThereFile( absolutePath ) );
      assertFalse( isThereFolder( absoluteFolderName ) );
      MyFileUtil.createFolder( absoluteFolderName );
      assertTrue( isThereFolder( absoluteFolderName ) );

      MyFileUtil.storeFile( absolutePath, data );
      assertTrue( isThereFile( absolutePath ) ); // Here

      MyFileUtil.removeFile( absolutePath );
      MyFileUtil.removeFolderIfEmpty( absoluteFolderName );
   }

   private boolean isThereFile( String fullpath )
   {
      final File file = new File( fullpath );
      if( false == file.exists() ) return false;
      if( file.isDirectory() ) return false;
      return true;
   }

   private boolean isThereFolder( String fullpath )
   {
      final File file = new File( fullpath );
      if( false == file.exists() ) return false;
      if( false == file.isDirectory() ) return false;
      return true;
   }

   @Test
   public void GetStructuredPathname()
   {
      final StructuredPathname sp = new MyFileUtil.StructuredPathname( "a/b/c" );
      assertEquals( "c", sp.getFilenameOnly() );
      assertEquals( 2, sp.getFullPathFoldernames().length );
      assertEquals( "a", sp.getFullPathFoldernames()[ 0 ] );
      assertEquals( "a/b", sp.getFullPathFoldernames()[ 1 ] );
   }

   @Test
   public void getSubPath1()
   {
      final String subPath = MyFileUtil.getSubPath( "/Usr", "/Usr/a/b.txt" );
      assertEquals( "a/b.txt", subPath );
   }

   @Test
   public void getSubPath2()
   {
      final String subPath = MyFileUtil.getSubPath( "/Usr/", "/Usr/a/b.txt" );
      assertEquals( "a/b.txt", subPath );
   }
}
